#include <my_header.h>

/* Usage:  */
// 实现自旋锁
pthread_mutex_t lock1;
void *threadFunc(void *arg)
{
    sleep(1);
    while(1)
    {
        int ret_lock = pthread_mutex_trylock(&lock1);
        if(ret_lock!=0)
        {
            // 继续尝试获得锁
            continue;
        }
        //访问共享资源
        //释放锁
    
        pthread_mutex_unlock(&lock1);
        break;
    }

    return NULL;
}
int main(void){                                  

    // 初始化锁
    pthread_mutex_init(&lock1, NULL);

    // 创建线程
    pthread_t tid;
    int ret_create = pthread_create(&tid, NULL, threadFunc, NULL);
    THREAD_ERROR_CHECK(ret_create, "pthread_create");

    pthread_mutex_lock(&lock1);
    printf("main is running...\n");
    sleep(5);
    pthread_mutex_unlock(&lock1);

    int ret_join = pthread_join(tid, NULL);
    THREAD_ERROR_CHECK(ret_join, "pthread_join");
    pthread_mutex_destroy(&lock1);

    return 0;
}


